package beerAir;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import java.util.Date;
import java.util.Calendar;
import java.util.List;

@ManagedBean
@SessionScoped
public class HomeView {
	private String depAirport;
	private String arrAirport;
	private Date depDate=Calendar.getInstance().getTime();
	private Date retDate=Calendar.getInstance().getTime();
	private int adult;
	private int child;
	private int infant;
	private int people;
	private boolean oneWay=false;
	private Date today= Calendar.getInstance().getTime();
	private String error;
		
	
	public String getError() {
		return error;
	}
	public void setError(String error) {
		this.error = error;
	}
	public int getPeople() {
		people=adult+child+infant;
		return people;
	}
	public void setPeople(int people) {
		this.people = people;
	}
	public int getAdult() {
		return adult;
	}
	public void setAdult(int adult) {
		this.adult = adult;
	}
	public int getChild() {
		return child;
	}
	public void setChild(int child) {
		this.child = child;
	}
	public int getInfant() {
		return infant;
	}
	public void setInfant(int infant) {
		this.infant = infant;
	}
	public boolean isOneWay() {
		return oneWay;
	}
	public void setOneWay(boolean oneWay) {
		this.oneWay = oneWay;
	}
	public String getDepAirport() {
		return depAirport;
	}
	public void setDepAirport(String depAirport) {
		this.depAirport = depAirport;
	}
	public String getArrAirport() {
		return arrAirport;
	}
	public void setArrAirport(String arrAirport) {
		this.arrAirport = arrAirport;
	}
	
	public Date getDepDate() {
		return depDate;
	}
	public void setDepDate(Date depDate) {
		this.depDate = depDate;
	}	
	public Date getRetDate() {
		return retDate;
	}
	public void setRetDate(Date retDate) {
		this.retDate = retDate;
	}
	@SuppressWarnings("deprecation")
	public String dateToString(Date d){
		int m = d.getMonth() + 1;
		int y = d.getYear() + 1900;
		String s = d.getDate() + "/" + m  + "/" + y;
		return s;
	}
	
	public String showTimetable() {
		if(checkArrAirport())
			error="ERROR:You have selected the same airport for departure and arrival.";
		if(checkDate())
			error="ERROR:You have selected an arrival date that is before the departure.";
		if (checkToday())
			error="ERROR:You have selected a departure date that is before today";
		if(error==null){
			return("Timetable");
		}
		return ("Home");
	}

	public boolean checkArrAirport() {
		if (depAirport.equals(arrAirport)) {
			return true;
		}
		return false;	
	}
	
	public boolean checkDate(){
		
		if(depDate.before(retDate)){
			return false;
		}
		return true;
	}
	
	@SuppressWarnings("deprecation")
	public boolean checkToday(){
				
		if((depDate.getYear()==today.getYear() && 
				depDate.getMonth()==today.getMonth() && 
				depDate.getDay()==today.getDay())|| depDate.after(today)){
			return false;
		}
		return true;
	}
	
	public List<Airport> getAllAirports(){
		AirportController a = new AirportController();
		return a.getAllAirports();
	}
	
}
